package concurrency;

import java.util.concurrent.atomic.AtomicLong;

/**
 * @Auther DRevelation
 * @Date 2020-06-11 下午 13:14
 * @Desc
 */
public class AtomicLongDemo {

    private static long aLong;
    private static AtomicLong atomicLong = new AtomicLong();
    private static Integer[] arrayOne = {0, 1, 2, 3, 0, 5, 6, 0, 56, 0};
    private static Integer[] arrayTwo = {10, 1, 2, 3, 0, 5, 6, 0, 56, 0};

    public static void main(String[] args) throws InterruptedException {
        Thread threadOne = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < arrayOne.length; i++) {
                    if (arrayOne[i] == 0) {
                        aLong++;
                        atomicLong.incrementAndGet();
                    }
                }
            }
        });

        Thread threadTwo = new Thread(() -> {
            for (int i = 0; i < arrayTwo.length; i++) {
                if (arrayTwo[i] == 0) {
                    aLong++;
                    atomicLong.incrementAndGet();
                }
            }
        });

        threadOne.start();
        threadTwo.start();

        threadOne.join();
        threadTwo.join();

        System.out.println("count 0:" + aLong);
        System.out.println("count 0:" + atomicLong.get());
    }
}
